package com.project.restaurant.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.mapper.UserMapper;
import com.project.restaurant.pojo.User;
import com.project.restaurant.redis.key.RedisKey;
import com.project.restaurant.redis.service.RedisClient;
import com.project.restaurant.thirdpart.bo.template.CodeTemplate;
import com.project.restaurant.thirdpart.mail.Mail;
import com.project.restaurant.thirdpart.mail.TemplateMail;
import com.project.restaurant.user.dto.EmailAuthCodeParam;
import com.project.restaurant.user.dto.LoginParam;
import com.project.restaurant.user.feign.MailService;
import com.project.restaurant.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LoginServiceImpl implements LoginService {


    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisClient redisClient;
    @Autowired
    MailService mailService;

    @Override
    public ServiceReturn<User> login(LoginParam loginParam) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",loginParam.getAccount())
                .eq("password",loginParam.getPassword());
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS, user);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_MATCH_USER, null);
    }

    @Override
    public ServiceReturn<User> emailLogin(EmailAuthCodeParam emailLoginParam) {
        String email = emailLoginParam.getEmail();
        String authCode = (String) redisClient.get(RedisKey.EMAIL_LOGIN_AUTH_CODE + email);
        if(authCode==null){
            return new ServiceReturn<>(ServiceStateCode.AUTH_CODE_TIMEOUT,null);
        }
        if(authCode.equals(emailLoginParam.getAuthCode())){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_email",email);
            User user = userMapper.selectOne(queryWrapper);
            redisClient.delKey(RedisKey.EMAIL_LOGIN_AUTH_CODE + email);
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,user);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_MATCH_USER, null);
    }

//    @Override
//    public ServiceReturn<String> generatorAuthCode(String sessionId) {
//        String authCode = String.valueOf((int) (Math.random() * 900000)+100000);
//        redisClient.setEx(RedisKey.LOGIN_AUTH_CODE+sessionId,authCode,120);
//        return new ServiceReturn<>(ServiceStateCode.SUCCESS,authCode);
//    }

    @Override
    public ServiceReturn<Boolean> sendAuthCodeToEmail(String email) {
        String code = (String) redisClient.get(RedisKey.EMAIL_LOGIN_AUTH_CODE + email);
        if (code != null) {
            return new ServiceReturn<>(ServiceStateCode.EMAIL_ALREADY_SEND,false);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_email",email);
        boolean exists = userMapper.exists(queryWrapper);
        if(exists){
            String authCode = String.valueOf((int) (Math.random() * 900000)+100000);
            Mail templateMail = new TemplateMail(email,"模板标题",
                    new CodeTemplate(email,authCode,"mail_login_auth_code"));
            ResponseResult<Boolean> result = mailService.sendTemplateMail(templateMail);
            redisClient.setEx(RedisKey.EMAIL_LOGIN_AUTH_CODE+email,authCode,120);
            try {
                return new ServiceReturn(result.getCode(),result.getData());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ServiceReturn<>(ServiceStateCode.FAILURE,false);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_EXIST_USER,false);
    }
}
